/** @param {NS} ns */
export function random_move(board, validMoves,ns) {
  const moveOptions = [];
  const size = board[0].length;

  // Look through all the points on the board
  for (let x = 0; x < size; x++) {
    for (let y = 0; y < size; y++) {
      // Make sure the point is a valid move
      const isValidMove = validMoves[x][y];
      // Leave some spaces to make it harder to capture our pieces
      const isNotReservedSpace = x % 2 || y % 2;

      if (isValidMove && isNotReservedSpace) {
        moveOptions.push([x, y]);
      }
    }
  }
  // Choose one of the found moves at random
  const randomIndex = Math.floor(Math.random() * moveOptions.length);
  // ns.tprint("moveOptions:",moveOptions,"\trandomIndex:",randomIndex);
  if(moveOptions.length)
  {
    // ns.tprint("返回值:",moveOptions[randomIndex]);
    return moveOptions[randomIndex];
  }
  return moveOptions[randomIndex] ?? [];
}

export function get_valid_moves(validMoves) 
{
  var result = [];
  const size = validMoves.length;
  for (let x = 0; x < size; x++) {
    for (let y = 0; y < size; y++) {
      if(validMoves[x][y])
        result.push([x,y]);
    }
  }
  return result;
}